<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>C# lexical structure</title>
<link rel="stylesheet" href="/cfg/format.css" type="text/css">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="keywords" content="C#, learn C#, lexical structure, .NET, Mono, programming, language">
<meta name="description" content="This part of the C# tutorial covers lexical structure of the C# language.">
<meta name="language" content="en">
<meta name="author" content="Jan Bodnar">
<meta name="distribution" content="global">

<script type="text/javascript" src="/lib/jquery.js"></script>
<script type="text/javascript" src="/lib/common.js"></script>

</head>

<body>

<div class="container">

<div id="wide_ad" class="ltow">
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* 160x600, August 2011 */
google_ad_slot = "2484182563";
google_ad_width = 160;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>



<div class="content">

<a href="/" title="Home">Home</a>&nbsp;
<a href="..">Contents</a>



<h1>C# lexical structure</h1>

<p>
Computer languages, like human languages, have a lexical structure. A source code
of a C# program consists of tokens. Tokens are atomic code elements. 
In C#, we have comments, variables, literals, operators, delimiters 
and keywords.
</p>

<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* NewSquare */
google_ad_slot = "0364418177";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script> 
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> 
</script> 

<p>
C# programs are composed of characters from the Unicode character set.
</p>

<h2>Comments</h2>

<p>
<b class="keyword">Comments</b> are used by humans to clarify the source code. 
There are three types of comments in C#. Single-line comments, multi-line comments
and XML comments. XML comments can be extracted to HTML files. 
</p>

<p>
Multi-line comments are enclosed by /* */ characters. Single line comments
start with two forward slashes.
</p>

<pre class="code">
using System;

/*
  This is comments.cs 
  Author: Jan Bodnar
  ZetCode 2010
*/

public class CSharpApp
{
    // Program starts here
    public static void Main()
    {
        Console.WriteLine("This is comments.cs");
    }
}
</pre>

<p>
Comments are ignored by the C# compiler.  
</p>

<pre>
/*
  This is comments.cs 
/*  Author: Jan Bodnar */
  ZetCode 2010
*/
</pre>

<p>
Comments cannot be nested. The above code does not compile. 
</p>


<h2>White space</h2>

<p>
White space in C# is used to separate tokens in the source file.
It is also used to improve readability of the source code. 
</p>

<pre>
int i = 0;
</pre>

<p>
White spaces are required in some places. For example between the <code>int</code> 
keyword and the variable name. In other places, it is forbidden. It cannot be present in
variable identifiers or language keywords.
</p>

<pre>
int a=1;
int b = 2;
int c  =  3;
</pre>

<p>
The amount of space put between tokens is irrelevant for the C# compiler. 
</p>




<h2>Variables</h2>

<p>
A <b class="keyword">variable</b> is an identifier, which holds a value. In programming 
we say, that we assign a value to a variable. Technically speaking, 
a variable is a reference to a computer memory, where the value is stored. 
Variable names can have alphanumerical characters and underscores. An identifier
may begin with a character or an underscore. It may not begin with a number.
Variable names are case sensitive. This means, that Name, name or NAME refer
to three different variables. Variable names also cannot match language keywords.
(In fact, we can use keywords as identifiers, if we precede them with @ character.
But it is not a good programming practice.)
</p>

<pre>
string name23;
int _col;
Date birth_date;
</pre>

<p>
These are valid C# identifiers.
</p>

<pre>
string 23name;
int %col;
Date birth date;
</pre>

<p>
These are invalid C# identifiers.
</p>

<pre class="code">
using System;


public class CSharpApp
{
    static void Main()
    {
        string name = "Robert";
        string Name = "Julia";

        Console.WriteLine(name);
        Console.WriteLine(Name);       
    }
}
</pre>

<p>
Identifiers are case sensitive. Name and name are two
different identifiers. In Visual Basic, a close cousin
of the C# language, this would not be possible. In this
language, variable names are not case sensitive.
</p>

<pre>
$ gmcs identifiers.cs
$ ./identifiers.exe 
Robert
Julia
</pre>


<h2>Literals</h2>

<p>
A <b class="keyword">literal</b> is a textual representation of a particular
value of a type. Literal types include boolean, integer, floating point, string, 
character, and date. Technically, a literal will be assigned a value 
at compile time, while a variable  will be assigned at runtime.
</p>

<pre>
int age = 29;
string nationality = "Hungarian";
</pre>

<p>
Here we assign two literals to variables. Number 29 and string "Hungarian" are literals. 
</p>

<pre class="code">
using System;


public class CSharpApp
{
    
    static void Main()
    {
        bool sng = true;
        string name = "James";
        string job = null;
        double weight = 68.5;
        DateTime born = DateTime.Parse("November 12, 1987");

        Console.WriteLine("His name is {0}", name);

        if (sng) 
        {
            Console.WriteLine("He is single");
        } else
        { 
            Console.WriteLine("He is in a relationship");
        }

        Console.WriteLine("His job is {0}", job);
        Console.WriteLine("He weighs {0} kilograms", weight);
        Console.WriteLine("He was born in {0}", 
            string.Format("{0:yyyy}", born));
    }
}
</pre>

<p>
In the above example, we have other literals. The bool literal 
may have value <code>true</code> or <code>false</code>.
<b>James</b> is a string literal.
The <code>null</code> represents the default value 
of any data type. <code>23</code> is an Integer literal. 
<b>68.5</b> is a floating point literal. 
Finally, the <b>November 12, 1987</b> is a date literal.
</p>

<pre>
$ ./literals.exe 
His name is James
He is single
His job is 
He weighs 68.5 kilograms
He was born in 1987
</pre>

<p>
This is the output of the program.
</p>


<h2>Operators</h2>

<p>
An <b class="keyword">operator</b> is a symbol used to 
perform an action on some value. Operators are used in 
expressions to describe operations involving one or 
more operands.
</p>

<pre>
+    -    *    /    %    ^    &amp;    |    !    ~
=    +=   -=   *=   /=   %=    ^=    ++    --
==   !=    &lt;   &gt;    &amp;=  &gt;&gt;=   &lt;&lt;=   &gt;=   &lt;= 
||   &amp;&amp;    &gt;&gt;    &lt;&lt;    ?:
</pre>

<p>
This is a partial list of C# operators. We will talk about operators later in the tutorial.
</p>

<h2>Separators</h2>

<p>
A <b class="keyword">separator</b> is a sequence of one or more characters used 
to specify the boundary between separate, independent regions in plain text or 
other data stream. 
</p>

<pre>
[ ]   ( )   { }   ,   :   ; 
</pre>

<pre>
string language = "C#";
</pre>

<p>
The double characters are used to mark the beginning and the end of a string. 
The semicolon (;) character is used to end each C# statement. 
</p>


<pre>
Console.WriteLine("Today is {0}", DateTime.Today.ToString("M/d"));
</pre>

<p>
Parentheses (round brackets) are used to mark the method signature. The signature consists 
of method parameters. Curly brackets are used to denote the evaluated value.
</p>

<pre>
int[] array = new int[5] {1, 2, 3, 4, 5};
</pre>

<p>
The square brackets [] are used to denote an array type. They are also used to access or
modify array elements. The curly brackets {} are also used to initiate arrays. The curly 
brackets are also used in variable interpolation or to enclose the body of a method or a class.
</p>

<pre>
int a, b, c;
</pre>

<p>
The comma character can be used to use multiple declarations on the 
same line of code. 
</p>


<h2>Keywords</h2>

<p>
A keyword is a reserved word in the C# language. Keywords are used 
to perform a specific task in the computer program. For example, define variables, 
do repetitive tasks or perform logical operations. 
</p>

<p>
C# is rich in keywords. Many of them will be explained in this
tutorial. The keywords include if, else, for, while, base, false,
float, catch, this and many others.
</p>


<pre class="code">
using System;


public class CSharpApp
{
    static void Main()
    {
        int i;

        for(i = 0; i&lt;= 5; i++)
        {
            Console.WriteLine(i);
        }
    }
}
</pre>

<p>
In the above example, we use several keywords. using, public,
static, void, int, for are C# keywords. 
</p>


<p>
In this part of the C# tutorial, we covered the basic lexis for the C#
language. 
</p>



<div class="botNav, center">
<span class="botNavItem"><a href="/">Home</a></span> ‡ <span class="botNavItem"><a href="..">Contents</a></span> ‡
<span class="botNavItem"><a href="#">Top of Page</a></span>
</div>


<div class="footer">
<div class="signature">
<a href="/">ZetCode</a> last modified October 12, 2010  <span class="copyright">&copy; 2007 - 2013 Jan Bodnar</span>
</div>
</div>

</div> <!-- content -->

</div> <!-- container -->

</body>
</html>

